package com.cgs.query.handler.render

import com.cgs.query.domain.BoundSql
import com.cgs.query.domain.ExecutionObj
import com.cgs.query.domain.QueryDefine
import com.cgs.query.domain.RenderType
import com.cgs.query.exception.QueryException
import com.cgs.query.executor.ExecutionContext
import com.cgs.query.handler.ParameterHandler

/**
 * 渲染适配器
 */
class MultipleParameterHandler : ParameterHandler {

    private val holder: Map<String, ParameterHandler> = mapOf(
            RenderType.NO.name to NoRenderParameterHandler(),
            com.cgs.query.domain.RenderType.QUESTION_MARK.name to QMakeParameterHandler(),
            com.cgs.query.domain.RenderType.FREEMARKER.name to FreemarkerParameterHandler(),
            com.cgs.query.domain.RenderType.MYBATIS.name to MybatisParameterHandler())

    override fun resolve(executionObj: ExecutionObj, param: Map<String, Any?>): BoundSql {
        val parameterHandler = holder[executionObj.renderType]
                ?: throw QueryException("不支持的渲染方式${executionObj.renderType}")
        return parameterHandler.resolve(executionObj, param)
    }

    override fun resolve(executionObj: ExecutionObj, param: Map<String, Any?>, executionContext: ExecutionContext): BoundSql {
        val parameterHandler = holder[executionObj.renderType]
                ?: throw QueryException("不支持的渲染方式${executionObj.renderType}")
        return parameterHandler.resolve(executionObj, param, executionContext)
    }

}